﻿// ******************************************************************************************************
// <copyright file="MyPlugin.cs" company="Caroso.inc">
//     Copyright (c) Carlos Osorio All rights reserved.
// </copyright>
// ******************************************************************************************************
// The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); 
// you may not use this file except in compliance with the License. You may obtain a copy of the License at 
// http:// www.mozilla.org/MPL/ 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF 
// ANY KIND, either express or implied. See the License for the specificlanguage governing rights and 
// limitations under the License. 
// 
// The Original Code is this . mapWindow 
// 
// The Initial Developer of this version of the Original Code is Carlos Osorio carosocali@gmail.com
// 
// Contributor(s): (Open source contributors should list themselves and their modifications here). 
// Change Log: 
// Date           Changed By      Notes
// 22 Nov 2010    Carlos Osorio   Inital upload 
// 17 Abr 2011    Carlos Osorio   
// ******************************************************************************************************

namespace GeostatisticalTool
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using DotSpatial.Data;
    using DotSpatial.Topology;
    using DotSpatial.Projections;
    using DotSpatial.Controls;
    using DotSpatial.Controls.Header;
    using GeostatisticalTool.Forms;
    using System.ComponentModel.Composition;
    using GeostatisticalTool.Classes;

    /// <summary>
    /// This is the start of the plug-in. In here all methods of the IPlugin interface are implemented.
    /// </summary>
    public class GeostatisticalToolPlugin : Extension
    {
        /// <summary>
        /// Name of the menu
        /// </summary>
        private const string MenuHeading = "kInterpolation_Methods";

        /// <summary>
        /// Name of the subMenu
        /// </summary>
        private const string SubMenu1 = "Kriging";


        public override void Activate()
        {
            AddHeaderRootItems();
            base.Activate();
        }

        public override void Deactivate()
        {
            App.HeaderControl.RemoveAll();
            base.Deactivate();
        }

        protected void AddHeaderRootItems()
        {
            App.HeaderControl.Add(new RootItem(MenuHeading, "Interpolation Tool"));
            IHeaderControl header = App.HeaderControl;
            SimpleActionItem geo = new SimpleActionItem(MenuHeading, "Geostatistical Methods", Kriging_Click);
            SimpleActionItem det = new SimpleActionItem(MenuHeading, "Deterministic Methods", Deterministical_Click);
            geo.LargeImage = GeostatisticalTool.Properties.Resources.grid32G;
            det.LargeImage = GeostatisticalTool.Properties.Resources.grid32;
            header.Add(geo);
            header.Add(det);
        }



        /// <summary>
        /// This method joins with the form called Select
        /// </summary>
        /// <param name="itemName">Get the item clicked</param>
        /// <param name="handled">Handled the item</param>
        public void Kriging_Click(object sender, EventArgs e)
        {
            Interpolators myForm = new Interpolators((Map)App.Map,false);
            // Select myForm = new Select(App.Map);
            myForm.Show();

        }

        /// <summary>
        /// This method joins with the form called Select
        /// </summary>
        /// <param name="itemName">Get the item clicked</param>
        /// <param name="handled">Handled the item</param>
        public void Deterministical_Click(object sender, EventArgs e)
        {
            DeterministicMethods myForm = new DeterministicMethods();
            myForm.Map = (Map)App.Map;
            // Select myForm = new Select(App.Map);
            myForm.Show();

        }
        /// <summary>
        /// This method joins with the form called Select
        /// </summary>
        /// <param name="itemName">Get the item clicked</param>
        /// <param name="handled">Handled the item</param>
        public void Comparison_Click(object sender, EventArgs e)
        {
            MessageBox.Show("No Implemented yet!");

        }


        [Export("GetKriginValues")]
        public double[] OpenInterpolator()
        {
            Interpolators myForm = new Interpolators((Map)App.Map, true);
            if (myForm.ShowDialog() == DialogResult.OK)
                return myForm.GetTheoreticalModelParameters();
            else
                return null;

        }


        /// <summary>
        /// This method execute a test of the Kriging plugin ( it´s needed a shapefile)
        /// It its needed to desable the menus
        /// </summary>
        public void Test()
        {
            ModelExponential model = new ModelExponential();
            model.Range = 700;
            model.C0 = 0;
            model.C1 = 300;

            IFeatureSet shapeLayer = FeatureSet.Open("C:/temp/puntos.shp");

            Kriging mkriging;
            mkriging = new Kriging(shapeLayer, 5, model);

            double height = Math.Abs(shapeLayer.Extent.MaxY - shapeLayer.Extent.MinY);
            double width = Math.Abs(shapeLayer.Extent.MaxX - shapeLayer.Extent.MinX);
            double defaultCellSize = -1;
            if (height <= width)
            {
                defaultCellSize = height / 50;
            }
            else
            {
                defaultCellSize = width / 50;
            }

            string salida = "C:/temp/salida.bil";
            MessageBox.Show("Cellsize:" + defaultCellSize.ToString() + " Projection: " + shapeLayer.Projection);
            mkriging.Surface(shapeLayer.Extent, defaultCellSize, shapeLayer.Projection, -32768, salida);
            MessageBox.Show("Ok..");

        }
    }
      
}
